package cn.iocoder.yudao.module.data.controller.admin.dstable;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.data.controller.admin.dstable.vo.*;
import cn.iocoder.yudao.module.data.dal.dataobject.dstable.DsTableDO;
import cn.iocoder.yudao.module.data.service.dstable.DsTableService;

@Tag(name = "管理后台 - table数据集")
@RestController
@RequestMapping("/data/ds-table")
@Validated
public class DsTableController {

    @Resource
    private DsTableService dsTableService;

    @PostMapping("/create")
    @Operation(summary = "创建table数据集")
    public CommonResult<Long> createDsTable(@Valid @RequestBody DsTableSaveReqVO createReqVO) {
        return success(dsTableService.createDsTable(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新table数据集")
    public CommonResult<Boolean> updateDsTable(@Valid @RequestBody DsTableSaveReqVO updateReqVO) {
        dsTableService.updateDsTable(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除table数据集")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteDsTable(@RequestParam("id") Long id) {
        dsTableService.deleteDsTable(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得table数据集")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<DsTableRespVO> getDsTable(@RequestParam("id") Long id) {
        DsTableDO dsTable = dsTableService.getDsTable(id);
        return success(BeanUtils.toBean(dsTable, DsTableRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得table数据集分页")
    public CommonResult<PageResult<DsTableRespVO>> getDsTablePage(@Valid DsTablePageReqVO pageReqVO) {
        PageResult<DsTableDO> pageResult = dsTableService.getDsTablePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DsTableRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出table数据集 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDsTableExcel(@Valid DsTablePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DsTableDO> list = dsTableService.getDsTablePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "table数据集.xls", "数据", DsTableRespVO.class,
                        BeanUtils.toBean(list, DsTableRespVO.class));
    }

}